# -*s- coding: utf-8 -*-
import urllib2, re, time, sys

if len(sys.argv) != 3:
    print 'python pywget.py spaceurl firstimagecode'
    sys.exit(1)

spaceurl = sys.argv[1]
imageList = []
d = {}
d['code'] = sys.argv[2]
d['indx'] = "1"
d['imgW'] = "400"
d['imgH'] = "600"
imageList.append(d)

def processPage(spaceurl, beginImageCode):
    page = urllib2.urlopen("http://hi.baidu.com/"+spaceurl+"/album/item/"+beginImageCode+".html/next")
    pageStr = ""
    while True:
        line = page.readline()
        if len(line) == 0:
            break
        pageStr += line.replace('\t', '').replace('\n', '')
    imageRawList = pageStr.split('},{')
    p=re.compile('.*imgID:"(.*)", picSn:(\d+), imgW:"(\d+)", imgH:"(\d+)"')
    for imageItem in imageRawList:
        m = p.search(imageItem)
        if m:
            d = {}
            d['code'] = m.groups()[0]
            d['indx'] = m.groups()[1]
            d['imgW'] = m.groups()[2]
            d['imgH'] = m.groups()[3]
            imageList.append(d)
    if -1 != pageStr.find('album_flag:"1"') :
        imageList.pop(-1)
        return False

    return True

while processPage(spaceurl, imageList[-1]['code']):
    pass
for imageInfo in imageList:
    print "%s index:%4d imgH:%5d imgW:%5d" % (imageInfo['code'], int(imageInfo['indx']), int(imageInfo['imgW']), int(imageInfo['imgH']))
